﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WarehouseManagement.BusinessLogic.DTO;
using WarehouseManagement.Presenters;
using WarehouseManagement.Presenters.Merchandise;
using WarehouseManagement.ViewModels;
using WarehouseManagement.ViewModels.Merchandise;

namespace WarehouseManagement.Views.Merchandise
{
    public partial class MerchandiseList : XtraForm, IView<MerchandiseListPresenter>, IMerchandiseListViewModel
    {
        private MerchandiseListPresenter _presenter;

        public MerchandiseListPresenter Presenter
        {
            get { return _presenter; }
        }

        public MerchandiseList()
        {
            InitializeComponent();
            _presenter = SetupPresenter();
        }

        public MerchandiseListPresenter SetupPresenter()
        {
            var p = new MerchandiseListPresenter();
            p.RegisterViewModel(this);
            return p;
        }

        public IEnumerable<MerchandiseDTO> Merchandises
        {
            get
            {
                return grdMerchandise.DataSource as IEnumerable<MerchandiseDTO>;
            }
            set
            {
                grdMerchandise.DataSource = value;

            }
        }

        private void MerchandiseListControl_Load(object sender, EventArgs e)
        {
            this.LoadGridViewData();
        }


        private void btnAddNew_Click(object sender, EventArgs e)
        {
            Presenter.AddMerchandise();
        }


        private void btnEdit_Click(object sender, EventArgs e)
        {
            Presenter.EditMerchandise();
        }



       
        public void LoadGridData()
        {
            this.LoadGridViewData();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {


            Presenter.DeleteMerchandises();
        }

        private void panelControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void grvMerchandise_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var obj = GetFocusObject();
            btnDelete.Enabled = btnEdit.Enabled = (obj != null);
        }

        private MerchandiseDTO GetFocusObject()
        {
            return grvMerchandise.GetFocusedRow() as MerchandiseDTO;
        }


        public MerchandiseDTO SelectedMerchandise
        {
            get
            {
                return GetFocusObject();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
}
